package com.xu.tools.config.mq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author xuguan
 * @date 2023/4/8
 * @desc
 */
@Slf4j
@Component
@RabbitListener(queues = {"sms.queue"})
public class SmsMqListener {

    @RabbitHandler
    public void smsHandler(String msg, Channel channel, Message message) throws Exception {
        try {
            log.info(">>>>>>>>>>>>>>>>监听到一个mq消息:{}<<<<<<<<<<<<<<", msg);
            TimeUnit.SECONDS.sleep(5);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
